package com.wzb.mybatisplus.bitaction.electrumaction.five;

import com.wzb.mybatisplus.bitaction.electrumaction.FileUtils;
import org.bitcoinj.core.*;
import org.bitcoinj.params.MainNetParams;
import java.math.BigInteger;
import java.util.concurrent.atomic.*;

public class BigIntegerKeyFinder {
    private static final NetworkParameters params = MainNetParams.get();
    private static String path = "D:\\btc\\miyao\\maney5.txt";
    private static FileUtils fileUtils = new FileUtils();
    public static void main(String[] args) throws InterruptedException {
        // 支持超大数字起始
        String startFrom = "1"; // 从10^18开始
        int targetCount = 5;
        int threadCount = Runtime.getRuntime().availableProcessors();

        AtomicReference<BigInteger> counter = new AtomicReference<>(new BigInteger(startFrom));
        AtomicInteger found = new AtomicInteger(0);

        for (int i = 0; i < threadCount; i++) {
            new Thread(() -> {
                while (found.get() < targetCount) {
                    BigInteger num = counter.getAndUpdate(bi -> bi.add(BigInteger.ONE));
                    try {
                        ECKey key = ECKey.fromPrivate(num);
                        String wif = key.getPrivateKeyAsWiF(params);
                        if (wif.startsWith("5")) {
                            synchronized (BigIntegerKeyFinder.class) {
                                if (found.get() < targetCount) {
                                    Address addr = LegacyAddress.fromKey(params, key);
                                    System.out.println(wif + " -> " + addr);
                                    fileUtils.writeFile("wif:"+wif+"addr:"+addr + "num:"+num,path );
                                    found.incrementAndGet();
                                }
                            }
                        }
                    } catch (Exception e) {}
                }
            }).start();
        }

        while (found.get() < targetCount) {
            Thread.sleep(1000);
        }
    }
}